package com.hefei.acl.controller;


import com.hefei.acl.entity.User;
import com.hefei.acl.service.IUserService;
import com.hefei.acl.vo.LoginVo;
import com.hefei.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author caizhengjun
 * @since 2021-03-05
 */
@RestController
@RequestMapping("/acl/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo) {

        String login = this.userService.login(loginVo);

        if (!StringUtils.isEmpty(login)) {
            Map<String, Object> map = new HashMap<>();
            map.put("token", "token");
            return R.ok().message("登录成功！").data(map);
        } else {
            return R.fail().message("登录失败！");
        }
    }

    @GetMapping("/info")
    public R getUserInfo(@PathParam("token") String token) {

        //TODO 解析token
        String userId = "1";
        User userInfo = this.userService.getById(userId);
        if (!StringUtils.isEmpty(userInfo)) {
            Map<String, Object> map = new HashMap<>();
            map.put("roles", "[roles]");
            map.put("name", "name");
            map.put("avatar", "https://guli-file-190513.oss-cn-beijing.aliyuncs.com/avatar/default.jpg");

            return R.ok().message("获取用户信息成功！").data(map);
        } else {
            return R.fail().message("获取用户信息失败！");
        }
    }

    @GetMapping("/count")
    public R countUser() {

        Long countUser = this.userService.countUser();

        Map<String, Object> map = new HashMap<>();
        map.put("countUser", countUser);

        return R.ok().message("获取用户数成功！").data(map);
    }
}


